module.exports = function ($scope, $resource, $modal, toaster) {

	//机构编号查询其游团列表,tag为true查数据不刷新圆圈
	$scope.search = function(pageNo, tag) {
		var para = {
			'pageNo': pageNo,
			'pageSize': $scope.itemsPerPage,
			'group_type': $scope.searchform.type,
			'model': $scope.searchform.model
		}
		if($scope.searchform.model == '2'){
			para.start_time = date2str2($scope.start_time.lable);
			para.end_time = date2str2($scope.end_time.lable);
			var time1 = parseInt(para.start_time.replace(/\-/g, ''));
			var time2 = parseInt(para.end_time.replace(/\-/g, ''));
			if(time1 > time2){
				toaster.warning({title: '', body: '开始时间不能大于结束时间'});
				return;
			}
		} else {
			para.start_time = $scope.searchform.today;
		}

		$scope.searchform.loading = tag;
		$scope.groupList = null;
		$resource('/api/ac/atc/groupService/getCompanyGroupList', {}, {}).save(para, function(res){
            if (res.errcode === 0) {
				$scope.groupList = res.data.results;
				$scope.totalItems = res.data.totalRecord;
				$scope.currentPage = pageNo;
				$scope.searchform.loading = $scope.groupList.length == 0 ? true : false;
            } else {
                toaster.error({title: '', body: res.errmsg});
            }
        });
	};

	init();
	//初始化几页数据并加载补贴规则
    function init() {
		$scope.currentPage = 1;			//当前页码
		$scope.itemsPerPage = 10;		//每页显示几条
		$scope.searchform = {
			'today': date2str2(new Date()),
			'model': '1',
			'model_type': [
				{'label': '实时查询', 'value': '1'},
				{'label': '历史查询', 'value': '2'},
			],
			'groupTypeList': [
				{'label': '全部', 'value': undefined},
				{'label': '一日游', 'value': '1'},
				{'label': '两日及多日游', 'value': '2'}
			]
		};
		$scope.start_time = {'lable': $scope.searchform.today, 'opened': false};
		$scope.end_time = {'lable': $scope.searchform.today, 'opened': false};
		$scope.search(1, true);
	};

	//查询游客名单
	$scope.personInfo = function(info) {
		var modalInstance = $modal.open({
			template: require('../views/groupTouristInfo.html'),
			controller: 'groupTouristInfo',
			size: 'lg',
			resolve: {
				info: function () {
					return info;
				}
			}
		});
	}

	//打开日历空间,选择时间
	$scope.dateOpen = function ($event, item) {
		$event.preventDefault();
		$event.stopPropagation();
		item.opened = true;
	};

	//日期转字符串
    function date2str2(d) {
        if (d === undefined) {
            return "";
        }
        if(typeof d === 'string'){
            return d;
        }
        var month = (d.getMonth() + 1).toString();
        var day = d.getDate().toString();
        if (month.length == 1) month = '0' + month;
        if (day.length == 1) day = '0' + day;
        return d.getFullYear() + "-" + month + "-" + day;
    }

};